package com.sx.zongzhi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.BusinessTestAdapter;
import com.sx.zongzhi.bean.BusinessBean;
import com.sx.zongzhi.bean.PopBusinessRoadBean;
import com.sx.zongzhi.bean.PopulationMoveDetailBean;
import com.sx.zongzhi.presenter.PopBusinessPest;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IBusinessView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XZY on 2017/11/28.
 * 流动人口业务信息添加或编辑
 */
public class BusinessMoveEditFragment extends Fragment implements IBusinessView{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private PopulationMoveDetailBean detailBean;
    private BusinessTestAdapter businessAdapter;
    private List<BusinessBean> businessBeanList=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyclerview,container,false);
        ButterKnife.bind(this,view);
        detailBean= (PopulationMoveDetailBean) getArguments().getSerializable("PopulationMoveDetailBean");
        initView();
        return view;
    }

    public List<String> submit(){
        if (businessAdapter==null){
            return null;
        }else {
            return businessAdapter.getSelectList();
        }
    }

    public List<PopBusinessRoadBean> submitRoad(){
        if (businessAdapter==null){
            return null;
        }else {
            return businessAdapter.getRoadList();
        }
    }

    private void initView() {
        new PopBusinessPest(this).getBusinessList();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        businessAdapter = new BusinessTestAdapter(getActivity(),businessBeanList);
        if (detailBean!=null){
            businessAdapter.selectData(detailBean.getBusinessInformationPerson(),detailBean.getRailways());
        }
        businessAdapter.setSelected(true);
        businessAdapter.setEmptyView(View.inflate(getActivity(),R.layout.load_error_view,null));
        recyclerView.setAdapter(businessAdapter);
    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),"获取业务信息失败!稍后再试");
    }

    @Override
    public void getBusinessView(List<BusinessBean> list) {
        if (!businessBeanList.isEmpty()){
            businessBeanList.clear();
        }
        businessBeanList.addAll(list);
        businessAdapter.notifyDataSetChanged();
    }
}
